<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Suitcategory as CategoryModel;
use app\common\model\Suit as SuitModel;

use app\common\model\Imgcategory as Imgcategory;
use app\common\model\Images as ImagesModel;

class Suit extends AdminBase
{
    protected $category_model;
    protected $suit_model;

    protected $imgcategory_model;
    protected $images_model;

    public function _initialize(){
        parent::_initialize();
        $this->category_model = new CategoryModel();
        $this->suit_model = new SuitModel();
        $this->imgcategory_model = new Imgcategory();
        $this->images_model = new ImagesModel();
        $category_level_list  = $this->category_model->getLevelList();
        $imgcategory_level_list  = $this->imgcategory_model->getLevelList();

        $this->assign('imgcategory_level_list', $imgcategory_level_list);
        $this->assign('suitcategory_level_list', $category_level_list);
    }

    public function index($cid = 0,$keyword = '',$page = 1){
        $map = [];

        if($cid > 0){
            $map['pid'] = ['eq',$cid];
        }

        if(!empty($keyword)){
            $map = [];
        }
        $img_list = $this->suit_model->where($map)->paginate(30,false,['page'=>$page]);

        // $img_list = $this->suit_model->where($map)->select();

        $this->assign('img_list',$img_list);
        return $this->fetch('index',[
            'keyword'=>$keyword,
            'page'=>$page,
            'cid'=>$cid,
        ]);
    }

    public function add(){
        return $this->fetch();
    }   

    public function save(){
        if($this->request->isPost()){
            $data = $this->request->post();
            if($this->category_model->where('id',$data['pid'])->value('pid') == 0){
                $this->error('请选择栏目');
            }
            if(!is_dir('./uploads/taotu/'.$data['pid'])){
                mkdir('./uploads/taotu/'.$data['pid']);
            }
            $img_data = [];
            $thumb_arr = [];
            if(array_key_exists('photo',$data)){
                foreach($data['photo'] as $v){
                    $image = \think\Image::open('.'.$v);
                    $height = $image->height(); 
                    $filename = '/uploads/taotu/'.$data['pid'].'/'.date('YmdHis').'_'.$data['pid'].rand(100,9999).'_thumb.png';
                    if($height > 400 ){
                        $image->water('./static/images/sy.png',1)->save('.'.$filename);
                    }else{
                        $image->water('./static/images/xsy.png',1)->save('.'.$filename);
                    }

                    $thumb_arr[] = $filename;                    
                }
            }else{
                $this->error('请上传图片');
            }

            $img_data = [
                'pid' => $data['pid'],
                'title' => $data['title'],
                'path' => serialize($data['photo']),
                'thumb' => serialize($thumb_arr),
            ];

            $result = $this->suit_model->allowField(true)->save($img_data);  
            if($result){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }
    }

    public function edit($id){
        $img_info = $this->suit_model->where('id',$id)->find();
        $this->assign('img_info',$img_info);
        return $this->fetch();
    }

    public function update($id){
        if($this->request->isPost()){
            $data = $this->request->post();
            if($this->category_model->where('id',$data['pid'])->value('pid') == 0){
                $this->error('请选择栏目');
            }
            if(!is_dir('./uploads/taotu/'.$data['pid'])){
                mkdir('./uploads/taotu/'.$data['pid']);
            }
            $img_data = [];
            $thumb_arr = [];
            if(array_key_exists('photo',$data)){
                foreach($data['photo'] as $v){
                    $image = \think\Image::open('.'.$v);
                    $height = $image->height(); 
                    $filename = '/uploads/taotu/'.$data['pid'].'/'.date('YmdHis').'_'.$data['pid'].rand(100,9999).'_thumb.png';
                    if($height > 400 ){
                        $image->water('./static/images/sy.png',1)->save('.'.$filename);
                    }else{
                        $image->water('./static/images/xsy.png',1)->save('.'.$filename);
                    }

                    $thumb_arr[] = $filename;                    
                }
            }else{
                $this->error('请上传图片');
            }

            $img_data = [
                'pid' => $data['pid'],
                'title' => $data['title'],
                'path' => serialize($data['photo']),
                'thumb' => serialize($thumb_arr),
            ];

            $result = $this->suit_model->allowField(true)->save($img_data,['id'=>['eq',$id]]);

            if($result){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
    }

    public function delete($id){
        $result = $this->suit_model->where('id',$id)->delete();
        if($result){
            return json(['msg'=>'删除成功']);
        }else{
            return json(['msg'=>'删除失败']);
        }
    }


    public function suit_img($cid = 0,$keyword = '',$page = 1){
        $map = [];

        if($cid > 0){
            $map['pid'] = ['eq',$cid];
        }

        if(!empty($keyword)){
            $map = [];
        }
        $img_list = $this->images_model->where($map)->paginate(90,false,['page'=>$page]);
        $this->assign('img_list',$img_list);
        return $this->fetch('suit_img',[
            'keyword'=>$keyword,
            'page'=>$page,
            'cid'=>$cid,
        ]);
    }
}